package com.daotingli.backendprograms.Repository.Author.booktype;

import com.daotingli.backendprograms.Bean.Bean.*;
import com.daotingli.backendprograms.Bean.Bean.AuthorType.AuthorTypeBookBean;
import com.daotingli.backendprograms.Bean.Bean.AuthorType.AuthorType_TUser;
import com.daotingli.backendprograms.Bean.Bean.AuthorType.Author_Book_PartBean;
import com.daotingli.backendprograms.Bean.Sum.Book_type_Name;
import com.daotingli.backendprograms.DB.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BookTypeRepository {
    @Autowired
    @Lazy //
    private BookTypeRepository bookTypeRepository;
    @Autowired
    private SpringDB  springDB;
    @Autowired
    private Book_type_Name book_type_name;
    @Autowired
    private AuthorType_TUser authorType_tUser;
    @Autowired
    private AuthorTypeBookBean bookBean;
    @Autowired
    private Book_partBean book_partBean;

    @Autowired
    private  Author_Book_PartBean Author_book_partBean;

    public int getAll(String username, String password) {
        String sql = "SELECT id FROM t_user WHERE username = ? AND password = ?";
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();

        try {
            return jdbcTemplate.queryForObject(sql, Integer.class, username, password);
        } catch (IncorrectResultSizeDataAccessException e) {
            // 当没有找到记录时返回 -1 或者其他默认值
            return -1;
        }
    }

    public List<String> getAllBooks(int id) {
        List<String> bookNames = new ArrayList<>();
        String sql = "SELECT book_name FROM book WHERE author_id = ?";
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
        try{

            jdbcTemplate.query(sql, (rs, rowNum) -> {
                bookNames.add(rs.getString("book_name"));
                return rs.getString("book_name");
            }, id);
        }catch (Exception e){
            return null;
        }

        return bookNames;
    }

    public List<Integer> getAuthorBookIDs(int id) {
        String sql = "SELECT id FROM book WHERE author_id = ?";
        List<Integer> bookIDs = new ArrayList<>();
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
        try{
            jdbcTemplate.query(sql, (rs, rowNum) -> {
                bookIDs.add(rs.getInt("id"));
                return rs.getInt("id");
            }, id);
        }catch (Exception e){
            return null;
        }
        return bookIDs;
    }

    public List<String> getBookTag(int id) {
        String sql = "SELECT booktypeid FROM book_type_view WHERE bookid = ?";
        String sql1 = "select id,type from book_type";
        List<String> bookTags = new ArrayList<>();
        List<Book_type_Name> list1 = new ArrayList<>();
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
        try{
            jdbcTemplate.query(sql, (rs, rowNum) -> {
                bookTags.add(rs.getString("booktypeid"));
                return rs.getString("booktypeid");
            }, id);
            jdbcTemplate.query(sql1, (rs, rowNum) -> {
                list1.add(new Book_type_Name(rs.getInt("id"),rs.getString("type")));
                return rs.getInt("id");
            });
            for(int i=0;i<bookTags.size();i++){
                for(int j=0;j<list1.size();j++){
                    if(bookTags.get(i).equals(list1.get(j).getId()+"")){
                        bookTags.set(i,list1.get(j).getType());
                    }
                }
            }
        }catch (Exception e){
            return null;

        }


        return bookTags;
    }
    /**
     * 获取作者全部信息
     * AuthorType_TUser
     * data:{
     *     "id": "作者id",
     *     "username": "作者名称",
     *     "password": "密码"  1
     *     "userphone": "手机号"
     *
     *      bookData:[{
     *           "id": "书籍id"  2
     *           "bookname": "书籍名称"
     *           "tagData:"[
     *              { "id": "标签id",
     *              "type": "标签"}] 3 4
     *           "chapterData[]": [
     *                {
     *                 章节id： "章节id",  5
     *                 "chapter": "章节名称"
     *                 }],
     *       },
     *         ......
     *     }
     * }
     *  以上为预期书籍格式
     */
    public List<AuthorType_TUser> getAuthorInfo(int id) { //作者id
        List<AuthorType_TUser> authorType_tUsers = new ArrayList<>();
        //查询 作者
        String sql = "SELECT * FROM t_user WHERE id = ?";
        List<t_userBean> t_userBeanList = new ArrayList<>();
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
        try {
            jdbcTemplate.query(sql, (rs, rowNum) -> {
                t_userBeanList.add(new t_userBean(rs.getInt("id"), rs.getString("username"), rs.getString("password"), rs.getInt("identity"), rs.getString("userphone"), rs.getInt("userstate")));
                return rs.getInt("id");
            }, id);
        } catch (Exception e) {
            return null;
        }
        //查询书籍
        String sql1 = "SELECT * FROM book WHERE author_id = ?";
        List<bookBean> bookBeanList = new ArrayList<>();
        try {
            jdbcTemplate.query(sql1, (rs, rowNum) -> {
                bookBeanList.add(new bookBean(rs.getInt("id"), rs.getString("author_id"), rs.getString("book_name"), rs.getString("book_state")));
                return rs.getInt("id");
            }, id);
        } catch (Exception e) {
            return null;
        }

        // 添加用户信息
        AuthorType_TUser author = new AuthorType_TUser(
                t_userBeanList.get(0).getId(),
                t_userBeanList.get(0).getUsername(),
                t_userBeanList.get(0).getPassword(),
                t_userBeanList.get(0).getIdentity(),
                t_userBeanList.get(0).getUserphone(),
                t_userBeanList.get(0).getUserstate(),
                new ArrayList<>()
        );
        authorType_tUsers.add(author);
        // 添加书籍信息
        for (bookBean book : bookBeanList) {
            author.getBooks().add(
                    new AuthorTypeBookBean(
                            book.getId(),
                            book.getBookname(),
                            book.getAuthor_id(),
                            book.getBook_state(),
                            new ArrayList<>(), // tags
                            new ArrayList<>() // parts
                    )
            );
        }
        // 查询每本书的标签
        String sqlType = "SELECT * FROM book_type_view WHERE bookid = ?";
        for (AuthorTypeBookBean book : author.getBooks()) {
            List<Book_type_viewBean> book_type_viewBeanList = new ArrayList<>();
            try {
                jdbcTemplate.query(sqlType, (rs, rowNum) -> {
                    book_type_viewBeanList.add(new Book_type_viewBean(
                            rs.getInt("id"),
                            rs.getInt("bookid"),
                            rs.getString("booktypeid")));
                    return rs.getInt("id");
                }, book.getId());
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            // 查询每个标签的详细信息
            String sqlTag = "SELECT id, type FROM book_type WHERE id = ?";
            for (Book_type_viewBean viewBean : book_type_viewBeanList) {
                List<book_typeBean> book_typeBeanList = new ArrayList<>();
                try {
                    jdbcTemplate.query(sqlTag, (rs, rowNum) -> {
                        book_typeBeanList.add(
                                new book_typeBean(
                                        rs.getInt("id"),
                                        rs.getString("type"
                                        )));
                        return rs.getInt("id");
                    }, Integer.parseInt(viewBean.getType()));
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                // 将标签添加到书籍中
                for (book_typeBean typeBean : book_typeBeanList) {
                    book.getTags().add(typeBean);
                }
            }
        }
            String sqlBookPart="SELECT id,bookid,part FROM book_part WHERE bookid = ?";
            List<Author_Book_PartBean> book_partBeanList = new ArrayList<>();
            try {

                for(int i=0;i<author.getBooks().size();i++){
                    jdbcTemplate.query(sqlBookPart, (rs, rowNum) -> {
                        book_partBeanList.add(new Author_Book_PartBean(
                                rs.getInt("id"),
                                rs.getInt("bookid"),
                                rs.getString("part")));
                        return rs.getInt("id");
                    }, author.getBooks().get(i).getId());
                }


            }catch (Exception e){
                    e.printStackTrace();
                }
        for (int i=0;i<author.getBooks().size();i++){
            for (int j=0;j<book_partBeanList.size();j++){
                if (author.getBooks().get(i).getId()==book_partBeanList.get(j).getBookid()){
                    author.getBooks().get(i).getParts().add(book_partBeanList.get(j));
                }
            }
        }


        return authorType_tUsers;
    }
}
